﻿using System;
using Platformer_Example.Controller.Character;

namespace Platformer_Example.Controller.Map
{
    public class MapCellData
    {
        public enum TerrainType
        {
            None = 0,
            Filler,
            Castle,
        }

        public enum StairType
        {
            None = 0,
            Stair11,
            Stair11Desc,
            Stair21,
            Stair21Desc,
            Stair31,
            Stair31Desc,
        }

        public enum DoorType
        {
            No = 0,
            Yes,
        }

        public enum LadderType
        {
            No = 0,
            Yes,
            HoverTerrain,
        }

        public enum MonsterType
        {
            None,
        }

        public bool IsChecked { get; set; }

        public TerrainType Terrain { get; private set; }
        public StairType Stair { get; private set; }
        private readonly DoorType _door;
        public LadderType Ladder { get; private set; }
        private readonly MonsterType _monster;

        public int Type { get; set; }

        public static MapCellData Filler
        {
            get { return new MapCellData(0x00000001); }
        }

        public bool IsTerrain
        {
            get
            {
                return Terrain != TerrainType.None &&
                       Ladder != LadderType.Yes &&
                       !IsChecked;
            }
        }

        public bool IsLadder
        {
            get { return Ladder != LadderType.No; }
        }

        public bool IsStair
        {
            get { return Stair != StairType.None; }
        }

        public MapCellData(int value)
        {
            if (value == 0x00FFFFFF)
            {
                Terrain = TerrainType.None;
                Stair = StairType.None;
                _door = DoorType.No;
                Ladder = LadderType.No;
                _monster = MonsterType.None;
            }
            else
            {
                //100000000000000000000000
                _door = (DoorType)((value & 0x00800000) >> 23);
                //011000000000000000000000
                Ladder = (LadderType)((value & 0x00600000) >> 21);
                //000111100000000000000000
                Stair = (StairType)((value & 0x001E0000) >> 17);
                //000000011111111100000000
                _monster = (MonsterType)((value & 0x0001FF00) >> 8);
                //000000000000000011111111
                Terrain = (TerrainType)(value & 0x000000FF);
            }

            IsChecked = false;
        }

        public bool IsSameTerrainType(MapCellData mapData)
        {
            return this.Type == mapData.Type && this.Terrain == mapData.Terrain && !mapData.IsChecked &&
                this.Ladder == mapData.Ladder;
        }
    }
}